/***************************************************************************
 *                                                                         *
 *   This file is part of the CrystalMem project,                          *
 *       http://code.google.com/p/crystalmem                               *
 *                                                                         *
 *   Copyright (C) 2008 by Alberto Scarpa <scarpa.alberto@gmail.com>       *
 *   Copyright (C) 2008 by Enrico Ros <enrico.ros@gmail.com>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef __GLGraphicsView_h__
#define __GLGraphicsView_h__

#include <QtCore/QTime>
#include <QtGui/QGraphicsView>
#include <QtGui/QGraphicsSceneDragDropEvent>

/**
    \class GLGraphicsView
    \brief GraphicsView over OpenGL with sane defaults and automatic background drawing
*/
class GLGraphicsView : public QGraphicsView {
    public:
        GLGraphicsView( const QString & backgroundSvgFile = ":/data/bg.svg", const QString & watermarkFile = ":/data/wm.png", QWidget * parentWidget = 0 );
        virtual ~GLGraphicsView();

        // types
        enum DragType { DragMove = 1, DragLeave = 2, DragDrop = 3 };

        // autorefresh functions
        void setAutoRefresh( int fps = 50 );
        void clearAutoRefresh();
        uint autoRefreshFps() const;

        // expose drag&drop functionality to subclasses
        virtual void viewDrag( DragType dragType, QGraphicsSceneDragDropEvent * event );
        // reimplementations can handle time-based updates in this function
        virtual void updateDynamics( double dT );
        // reimplementations can watch for pressed keys here
        virtual void keyPressed( const QString & keyText );

    protected:
        // ::QGraphicsView
        void resizeEvent( QResizeEvent * event );
        void drawBackground( QPainter * /*painter*/, const QRectF & /*rect*/ );

    private:
        // background (renderer/cache/timing)
        bool                    m_inited;
        QTime                   m_updateTime;
        QTime                   m_fpsTime;
        int                     m_fpsCounter;
        QString                 m_backName;
        class QSvgRenderer *    m_backRenderer;
        class QPixmap *         m_backPixmap;
        class QPixmap *         m_watermarkPixmap;
        class QTimer *          m_refreshTimer;
};

#endif
